package com.yangaobo.mapper;

import com.yangaobo.dto.EmpPageDTO;
import com.yangaobo.entity.Emp;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author 杨奥博
 */
public interface EmpMapper {
    @Insert("""
            insert into ums_emp(`username`, `password`, `avatar`, `phone`, `wechat`, `email`, `gender`, `age`, `province`, 
            `address`, `realname`, `idcard`, `info`, `fk_dept_id`, `hiredate`, `deleted`, `created`, `updated`)
            values (#{username}, #{password}, #{avatar}, #{phone}, #{wechat}, #{email}, #{gender}, #{age}, #{province}, 
            #{address}, #{realname}, #{idcard}, #{info}, #{fkDeptId}, #{hiredate}, #{deleted}, #{created}, #{updated})
            """)
    int insert(Emp emp);

    @Results(
            id = "empResultMap",
            value = {
                    @Result(property = "id",column = "id",id = true),
                    @Result(property = "fkDeptId",column = "fk_dept_id"),
                    @Result(property = "dept",column = "fk_dept_id",one = @One(select = "com.yangaobo.mapper.DeptMapper.select")),
            }
    )
    @Select("""
            select * from ums_emp t where t.id = #{param1} and t.deleted = 0
            """)
    Emp select(Long id);

    @ResultMap("empResultMap")
    @Select("""
            <script>
            select * from ums_emp t
           <where>
                <if test='username != null'> username = #{username} and </if>
                <if test='phone != null'> phone = #{phone} and </if>
                <if test='idcard != null'> idcard = #{idcard} and </if>
                <if test='realname != null'> realname = #{realname} and </if>
                <if test='fkDeptId != null'> fk_dept_id = #{fkDeptId} and </if>
                t.deleted = 0
            </where>
            </script>
            """)
    List<Emp> list(EmpPageDTO dto);

    @Update("""
            <script>
            update ums_emp
            <set>
                <if test="username != null">username = #{username},</if>
                <if test="password != null">password = #{password},</if>
                <if test="avatar != null">avatar = #{avatar},</if>
                <if test="phone != null">phone = #{phone},</if>
                <if test="wechat != null">wechat = #{wechat},</if>
                <if test="email != null">email = #{email},</if>
                <if test="gender != null">gender = #{gender},</if>
                <if test="age != null">age = #{age},</if>
                <if test="province != null">province = #{province},</if>
                <if test="address != null">address = #{address},</if>
                <if test="idcard != null">idcard = #{idcard},</if>
                <if test="realname != null">realname = #{realname},</if>
                <if test="info != null">info = #{info},</if>
                <if test="fkDeptId != null">fk_dept_id = #{fkDeptId},</if>
                <if test="hiredate != null">hiredate = #{hiredate},</if>
                <if test="deleted != null">deleted = #{deleted},</if>
                <if test="created != null">created = #{created},</if>
                <if test="updated != null">updated = #{updated},</if>
                version=version+1
            </set>
            where id = #{id} and deleted = 0 and version = #{version}
            </script>
            """
    )
    int update(Emp emp);

    @Update("""
            update ums_emp set deleted = 1, updated = current_timestamp where id = #{id}
            """)
    int delete(Long id);

    @Update("""
            <script>
            update ums_emp set deleted = 1, updated = current_timestamp where
            <foreach collection='list' item='e' open='id in (' close= ')' separator=','>
                #{e}
            </foreach>
            </script>
            """)
    int deleteBatch(List<Long> ids);

    @Update("""
            update ums_emp set password = #{param1}, updated = current_timestamp
            where password = #{param2} and id = #{param3} and deleted = 0
            """)
    int updatePassword(String newPassword, String oldPassword, Long id);

    @ResultMap("empResultMap")
    @Select("""
            select * from ums_emp t 
            where t.username = #{param1} and t.password = #{param2} and t.deleted = 0
            """)
    Emp selectByAccount(String username, String password);
}
